<template>
  <CompOperate
      :field="field"
      :designer="designer"
      :design-state="designState"
      :parent-list="parentList"
      :parent-widget="parentWidget"
      :sub-form-row-id="subFormRowId"
      :sub-form-row-index="subFormRowIndex"
      :sub-form-col-index="subFormColIndex"
      :index-of-parent-list="indexOfParentList"
      :display-style="field['options'].displayStyle"
  >
    <div class="scene-head-widget" :style="getStyle('head')">
      <template v-if="true">
        <div class="logo-wrap item" :style="getStyle('picture')">
          <template v-if="(!!(optionsInfo?.imageUrl?.val) || (optionsInfo?.imageUrl))">
            <img class="logo" :src="pictureResource" alt="企业Logo"/>
          </template>
        </div>
        <template v-if="(optionsInfo.contentShow)">
          <div class="title item"
               :style="getStyle('title')"
               v-if="!!(optionsInfo.content || headTitle)"
               :data-content="(optionsInfo.content || headTitle)"
          >
            <NoticeBar :text="(optionsInfo.content || headTitle)" :style="deptStyle"/>
          </div>
        </template>


        <div class="qrcodeBigBox">
          <div class="qrcodeBox" v-if="QrCodeResource1">
            <div class="qrcodeBoxImg">
              <img :src="QrCodeResource1" alt="二维码" class="qrcodeBoxImgStand"/>
            </div>
            <div class="qrcodeBoxName">
              {{ (optionsInfo.qrcodeUrl1Name || '') }}
            </div>
          </div>
          <div class="qrcodeBox" v-if="QrCodeResource2">
            <div class="qrcodeBoxImg">
              <img :src="QrCodeResource2" alt="二维码" class="qrcodeBoxImgStand"/>
            </div>
            <div class="qrcodeBoxName">
              {{ (optionsInfo.qrcodeUrl2Name || '') }}
            </div>
          </div>
        </div>
        <!-- <template v-if="optionsInfo['viewMode']===2">
          <div class="item viewMode-2">
            <div class="time view-item">{{ time }}</div>
            <div class="week view-item">{{ getWeekInfo(week) }}</div>
            <div class="date view-item">{{ date }}</div>
          </div>
        </template>
        <template v-else>
          <div class="item viewMode-1" :class="timeClassName(optionsInfo.changePosition)">
            <div class="time">{{ time }}</div>
            <div class="sec" v-if="(optionsInfo.dateShow)">
              <div class="week">{{ getWeekInfo(week) }}</div>
              <div class="date">{{ date }}</div>
            </div>
          </div>
        </template> -->
      </template>
    </div>
  </CompOperate>
</template>

<script>
import dayjs from 'dayjs';

import {NoticeBar} from 'vant';
import emitter from "@/utils/designer/emitter";
import {getWeek, pxToVw} from '@/utils/common';
import CompOperate from "@c/public/form-designer/devType/modules/compOperate.vue";
import mixin from "@sc/templateDetails/modules/homeMiddle/fieldMixin";
import {buildParentApi} from "@/utils/designer/util";
import ScrollX from "@c/public/seamlessScroll/ScrollX.vue";

let buildParent = buildParentApi();

export default {
  name: "scene-head-QRcode-widget",

  componentName: "FieldWidget", //必须固定为FieldWidget，用于接收父级组件的broadcast事件

  mixins: [emitter, mixin],

  components: {
    ScrollX,
    CompOperate,
    NoticeBar,
  },

  props: {
    widget: Object,
    field: Object,
    parentWidget: Object,
    parentList: Array,
    indexOfParentList: Number,
    designer: Object,
    designState: {
      type: Boolean,
      default: false,
    },
    devType: {
      type: Boolean,
      default: () => {
        return false
      },
    },
    subFormRowIndex: {
      /* 子表单组件行索引，从0开始计数 */
      type: Number,
      default: -1,
    },

    subFormColIndex: {
      /* 子表单组件列索引，从0开始计数 */ type: Number,
      default: -1,
    },

    subFormRowId: {
      /* 子表单组件行Id，唯一id且不可变 */ type: String,
      default: "",
    },

    headTitle: {
      type: String,
      default: "",
    },

    visibleTitle: {
      type: Boolean,
    },

    //prop传入的表单JSON配置
    parentApi: {
      type: Object,
      default: () => buildParentApi()
    },
    widgetKey: {
      type: String,
    }
  },

  data() {
    return {
      week: '',
      split: 0.5,
      split2: 0.5,
      optionsInfo: Object(),
      mockOpen: false,
      activation: false,
      date: dayjs().format('YYYY年MM月DD日'),
      time: dayjs().format('HH:mm'),
      leftOffset: 30,
      triggerDragging: false,
    }
  },

  computed: {
    timeClassName() {
      return function (val) {
        return val ? 'date-time' : 'time-date'
      }
    },

    deptStyle() {
      return {
        fontWeight: this.optionsInfo.deptFontWeight,
        color: this.optionsInfo.deptFontColor,
        fontSize: pxToVw(this.optionsInfo.deptFontSize)
      }
    },

    timeType() {
      return this.optionsInfo.timeValue
    },

    // 获取日期
    getWeekInfo() {
      return function (info) {
        return getWeek(info)
      }
    },

    // 设置的行内样式
    getStyle() {
      return function (val) {
        const self = this;
        let info = (self.optionsInfo) || Array();
        let Obj = Object();
        switch (val) {
          case 'picture':
            Reflect.set(Obj, 'height', pxToVw(info.imageHeight));
            Reflect.set(Obj, 'width', pxToVw(info.imageWidth));
            Reflect.set(Obj, 'borderRadius', pxToVw(info.imageRadius));
            return Obj;
          case 'title':
            Reflect.set(Obj, 'fontSize', pxToVw(info.fontSize));
            return Obj;
          case 'head':
            Reflect.set(Obj, 'height', pxToVw(info.headHeight));
            Reflect.set(Obj, 'backgroundColor', `${(info.backgroundColor)}`);
            Reflect.set(Obj, 'color', `${(info.fontColor)}`);
            return Obj;
          case 'split':
            if (info?.contentShow) {
              Reflect.set(Obj, 'flex', 3);
            } else {
              Reflect.set(Obj, 'flex', 1);
            }
            return Obj;
        }
      }
    },

    //获取LOGO资源
    pictureResource() {
      let {imageUrl} = this.optionsInfo;
      if (this.$lodash.isObject(imageUrl)) {
        return imageUrl?.val?.url || imageUrl?.url;
      } else {
        return imageUrl;
      }
    },

    //获取二维码资源
    QrCodeResource1() {
      let {qrcodeUrl1} = (this.optionsInfo);
      console.log('this.optionsInfo', this.optionsInfo)
      if (qrcodeUrl1) {
        return qrcodeUrl1
      } else {
        return ''
      }
    },
    //获取二维码资源
    QrCodeResource2() {
      let {qrcodeUrl2} = (this.optionsInfo);
      if (qrcodeUrl2) {
        return qrcodeUrl2
      } else {
        return ''
      }
    },

    // 监听详情
    deptShown() {
      let templateInfo = localStorage.getItem('templateInfo');
      try {
        let info = JSON.parse(templateInfo);
        let resolutionArr = this.$lodash.split((info.resolution), '*')
        if (resolutionArr?.length && (resolutionArr.length) >= 2) {
          return resolutionArr[0] > resolutionArr[1];
        }
      } catch (err) {
        console.log('数据异常')
      }
    }
  },

  created() {
    this.registerToRefList();
    this.initEventHandler();
    this.handleOnCreated();
  },

  mounted() {
    this.updateTime();
    this.handleOnMounted();
  },

  methods: {
    // 获取时间
    updateTime(val) {
      let timeType = (this.optionsInfo.timeValue);
      if (!!val) {
        let msg = JSON.parse(val);
        if (!!msg && !!(msg.timestamp)) {
          let timestamp = dayjs.unix(msg.timestamp);
          [this.week, this.date, this.time] = dayjs(timestamp).format(`d,${timeType},HH:mm`).split(',');
        } else {
          [this.week, this.date, this.time] = dayjs().format(`d,${timeType},HH:mm`).split(',');
        }
      } else {
        [this.week, this.date, this.time] = dayjs().format(`d,${timeType},HH:mm`).split(',');
      }
    },

    handleResize() {
      console.log('拖动数据的变化')
    },

    handleChange(val, vmid) {
      console.log("val,vmid", val, vmid)
    },


    // 接收父组件传递过来的数据
    executeMethod(key, data) {
      const self = this;
      switch (key) {
        case "headerInfo":
          self.updateTime(data);
          break
      }
    }
  },

  destroyed() {
    this.unregisterFromRefList();
  },

  watch: {

    // 获取配置项
    field: {
      handler: function (val) {
        let key = val.options.config
        this.optionsInfo = val.options[key];
      },
      deep: true,
      immediate: true
    },

    designer: {
      handler: function (val) {
        const self = this;
        self.activation = val?.selectedId === this.widgetKey;
      },
      deep: true,
      immediate: true
    },

    // 监听时间更新
    timeType: {
      handler: function (val) {
        if (val) {
          this.updateTime()
        }
      },
      deep: true,
      immediate: true
    },

  }

};
</script>

<style lang="scss" scoped>
.scene-head-widget {
  padding: 0 30px;
  box-sizing: border-box;
  background-color: #fff;
  display: flex;
  justify-content: space-between;
  color: #333;
  height: 150px;
  width: 100%;
  overflow: hidden;

  .qrcodeBigBox {
    display: flex;
    align-items: center;
  }

  .qrcodeBox {
    width: 100px;
    height: 125px;
    padding: 7px;
    background: #E8E8E7;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    margin-left: 22px;
  }

  .qrcodeBoxImg {
    width: 86px;
    height: 86px;
    // background: orange;
  }

  .qrcodeBoxImgStand {
    width: 100%;
    height: 100%;
  }

  .qrcodeBoxName {
    margin-top: 10px;
  }

  .item {
    flex: 1;
  }

  .split-area {
    flex: 3;
  }

  .logo-wrap {
    display: block;
    overflow: hidden;
    align-self: center;

    img {
      height: 100% !important;
      max-height: 90px !important;
      max-width: 100% !important;
      object-fit: contain;
    }
  }

  .title {
    flex: 2;
    text-align: center;
    font-size: 80px;
    height: 100%;
    line-height: 1;
    box-sizing: border-box;
    align-self: center;
    //text-shadow: -1px 1px #BBB,
    //-2px 2px #BBB,
    //-3px 3px #BBB,
    //-4px 4px #BBB,
    //-5px 5px #BBB,
    //-7px 7px 3px #0008;
    align-items: center;

    :deep(.van-notice-bar) {
      background: transparent;
      color: black;
      height: 100%;
      width: 100%;
      // font-size: 68px;
      line-height: 90px;

      .van-notice-bar__wrap {
        height: 100%;
        margin: 0 auto;
        display: flex;
        justify-content: center;

        > div {
          @include fontWeight();
        }

      }
    }
  }

  //视图一
  .viewMode-1 {
    overflow: hidden;
    line-height: 1;
    display: flex;
    flex-wrap: nowrap;
    justify-content: flex-end;

    .time {
      float: right;
      margin-right: 8px;
      font-size: 90px;
      vertical-align: top;
      white-space: nowrap;
      box-sizing: border-box;
      align-self: center;
      padding-right: 20px;
    }

    .sec {
      float: right;
      align-self: center;
      font-size: 32px;

      .week {
        text-align: right;
        white-space: nowrap;
      }

      .date {
        font-weight: 500;
        white-space: nowrap;
        box-sizing: border-box;
        margin-top: 10px;
      }
    }
  }

  //视图一:时间日期交换位置
  .date-time {
    .time {
      order: 1;
      padding-right: 0;
    }

    .sec {
      margin-right: 8px;
      padding-right: 20px;
    }
  }

  //视图二
  .viewMode-2 {
    font-size: 32px;
    display: flex;
    align-items: center;
    justify-content: right;

    .view-item {
      margin-right: 16px;
      box-sizing: border-box;

      &:last-child {
        margin-right: 0;
      }
    }
  }

}

//面板
:deep(.first-split,.second-split) {
  height: 100%;
  width: 100%;
  overflow: hidden;
  outline: 1px solid #dcdee2;

  &:hover {
    outline: 1px solid #5cadff !important;
    box-sizing: border-box;

    .ivu-split-trigger-con {
      display: block !important;

      .ivu-split-trigger-vertical {
        background: #5cadff;
      }

      .ivu-split-trigger-bar {
        background: white;
      }
    }
  }

  .first-split-pane {
    height: 100%;
    padding: 10px;
    overflow: hidden;
  }

  .first-split-pane.no-padding {
    height: 200px;
    padding: 0;
  }

  .ivu-split-wrapper {
    position: relative;
    width: 100%;
    height: 100%
  }

  .ivu-split-pane {
    position: absolute
  }

  .ivu-split-pane.left-pane, .ivu-split-pane.right-pane {
    top: 0;
    bottom: 0
  }

  .ivu-split-pane.left-pane {
    left: 0
  }

  .ivu-split-pane.right-pane {
    right: 0
  }

  .ivu-split-pane.bottom-pane, .ivu-split-pane.top-pane {
    left: 0;
    right: 0
  }

  .ivu-split-pane.top-pane {
    top: 0
  }

  .ivu-split-pane.bottom-pane {
    bottom: 0
  }

  .ivu-split-pane-moving {
    -webkit-user-select: none;
    -moz-user-select: none;
    -ms-user-select: none;
    user-select: none
  }

  .ivu-split-trigger {
    border: 1px solid #dcdee2
  }

  .ivu-split-trigger-con {
    position: absolute;
    -webkit-transform: translate(-50%, -50%);
    -ms-transform: translate(-50%, -50%);
    transform: translate(-50%, -50%);
    z-index: 10;
    display: none !important;
  }

  .ivu-split-trigger-bar-con {
    position: absolute;
    overflow: hidden;
  }

  .ivu-split-trigger-bar-con.vertical {
    left: 1px;
    top: 50%;
    height: 32px;
    -webkit-transform: translate(0, -50%);
    -ms-transform: translate(0, -50%);
    transform: translate(0, -50%)
  }

  .ivu-split-trigger-bar-con.horizontal {
    left: 50%;
    top: 1px;
    width: 32px;
    -webkit-transform: translate(-50%, 0);
    -ms-transform: translate(-50%, 0);
    transform: translate(-50%, 0)
  }

  .ivu-split-trigger-vertical {
    width: 6px;
    height: 100%;
    background: #f8f8f9;
    border-top: none;
    border-bottom: none;
    cursor: col-resize
  }

  .ivu-split-trigger-vertical .ivu-split-trigger-bar {
    width: 4px;
    height: 1px;
    background: rgba(23, 35, 61, .25);
    float: left;
    margin-top: 3px
  }

  .ivu-split-trigger-horizontal {
    height: 6px;
    width: 100%;
    background: #f8f8f9;
    border-left: none;
    border-right: none;
    cursor: row-resize
  }

  .ivu-split-trigger-horizontal .ivu-split-trigger-bar {
    height: 4px;
    width: 1px;
    background: rgba(23, 35, 61, .25);
    float: left;
    margin-right: 3px
  }

  .ivu-split-horizontal > .ivu-split-trigger-con {
    top: 50%;
    height: 100%;
    width: 0
  }

  .ivu-split-vertical > .ivu-split-trigger-con {
    left: 50%;
    height: 0;
    width: 100%
  }

  .ivu-split .no-select {
    -webkit-touch-callout: none;
    -webkit-user-select: none;
    -moz-user-select: none;
    -ms-user-select: none;
    user-select: none
  }
}

:deep(.split-area-pro) {
  outline: none !important;

  .ivu-split-trigger-con {
    display: none;
  }
}

.split-drag-area {
  width: 100%;
  height: 100%;
}

</style>
